﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Rendering;
using UnityEngine;

public class MenuItem_MaterialEdtior : EditorWindow
{
    public GameObject materialGameObject;
    public Shader useShader;

    [MenuItem("Miao/Material/批量模型中的着色器", false, 3)]
    public static void ShowEditor()
    {
        EditorWindow.GetWindow<MenuItem_MaterialEdtior>(false, "批量替换着色器", true).Show();
    }
    private void OnGUI()
    {
        materialGameObject = (GameObject)EditorGUILayout.ObjectField("模型单位", materialGameObject, typeof(GameObject), false);
        useShader = (Shader)EditorGUILayout.ObjectField("使用的新Shader", useShader, typeof(Shader), false);


        if (GUILayout.Button("开始转移着色", EditorStyles.miniButtonMid))
        {
            string saveFolorPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(materialGameObject));
            //SaveMat(saveFolorPath);
            //Create(materialGameObject);
            CreatePrefab(materialGameObject);

            AssetDatabase.SaveAssets();
        }
    }
    void SaveMat(string SavePath)
    {
        Material newMaterial = new Material(useShader);
        AssetDatabase.CreateAsset(newMaterial, Path.Combine(SavePath, $"save.mat"));


    }

    void CreatePrefab(GameObject gameObject)
    {
        GameObject useObject = (GameObject)PrefabUtility.InstantiatePrefab(gameObject);

        string saveFolorPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(gameObject));
        string namePath = Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(gameObject));

        var cashe = PrefabUtility.SaveAsPrefabAsset(useObject, Path.Combine(saveFolorPath, $"{namePath}_Copy.prefab"));
        GameObject.DestroyImmediate(useObject);
        useObject = cashe;
        string materialSavePath = AssetDatabase.CreateFolder(saveFolorPath, namePath);
        materialSavePath = AssetDatabase.GUIDToAssetPath(materialSavePath);

        if (useObject.TryGetComponent<Renderer>(out Renderer mainRenderer))
        {
            List<Material> materialList = new List<Material>();
            AddMaterial(ref materialList, mainRenderer, materialSavePath);
            mainRenderer.SetMaterials(materialList);
        }
        Renderer[] meshRenderers = useObject.GetComponentsInChildren<Renderer>();

        for (int i = 0; i < meshRenderers.Length; i++)
        {
            List<Material> materialList = new List<Material>();
            AddMaterial(ref materialList, meshRenderers[i], materialSavePath);
            meshRenderers[i].SetMaterials(materialList);
        }
        AssetDatabase.SaveAssets();

    }
    private void AddMaterial(ref List<Material> materials, Renderer renderer, string materialSavePath)
    {
        for (int i = 0; i < renderer.sharedMaterials.Length; i++)
        {
            string name = renderer.sharedMaterials[i].name;
            bool jump = false;
            for (int j = 0; j < materials.Count; j++)//重复贴图直接添加
            {
                if (materials[j].name == name)
                {
                    materials.Add(materials[j]);
                    jump = true;
                    break;
                }
            }
            if (!jump)
            {
                Material newMaterial = new Material(useShader);
                newMaterial.CopyMatchingPropertiesFromMaterial(renderer.sharedMaterials[i]);
                newMaterial.name = name;
                AssetDatabase.CreateAsset(newMaterial, Path.Combine(materialSavePath, $"{name}.mat"));
                materials.Add(newMaterial);
            }
        }
    }


    void Create(GameObject gameObject)
    {
        GameObject UseObject = (GameObject)PrefabUtility.InstantiatePrefab(gameObject);
        var createRender = UseObject.GetComponent<MeshRenderer>();
        //var mainRender = gameObject.GetComponent<MeshRenderer>();


        string saveFolorPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(gameObject));
        string namePath = Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(gameObject));


        string materialSave = AssetDatabase.CreateFolder(saveFolorPath, namePath);
        materialSave = AssetDatabase.GUIDToAssetPath(materialSave);

        List<Material> materials = new List<Material>();
        for (int i = 0; i < 1; i++)
        {
            Material newMaterial = new Material(useShader);
            //Material newMaterial = new Material(createRender.sharedMaterials[i]);
            string matName = createRender.materials[i].name;

            //newMaterial.shader = useShader;

            materials.Add(newMaterial);
            AssetDatabase.CreateAsset(newMaterial, Path.Combine(materialSave, $"123.mat"));


            //mainRender.SetMaterials();


        }
        createRender.SetMaterials(materials);

        AssetDatabase.CreateAsset(UseObject, Path.Combine(saveFolorPath, $"{namePath}_Copy.GameObject"));
        AssetDatabase.SaveAssets();
    }
}